package com.example.rotateimageview

import android.animation.*
import android.content.Intent
import android.graphics.BitmapFactory
import android.os.Bundle
import android.os.Environment
import android.os.Environment.getExternalStorageDirectory
import android.os.storage.StorageManager
import android.os.storage.StorageManager.ACTION_MANAGE_STORAGE
import android.util.Log
import android.view.View
import android.view.ViewTreeObserver.OnGlobalLayoutListener
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.example.rotateimageview.MotionLayout.MotionDemoActivity
import com.example.rotateimageview.compose.ComposeActivity
import com.example.rotateimageview.infiniteVp.InfiniteAdapter
import com.example.rotateimageview.previewImageDemo.PreviewListActivity
import com.example.rotateimageview.revealeffect.RevealEffectActivity
import com.example.rotateimageview.rotateImage.RotateImage
import io.flutter.embedding.android.FlutterActivity
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.map
import rx.Observable
import rx.schedulers.Schedulers
import java.util.*
import java.util.concurrent.Executors
import kotlin.random.Random


class MainActivity : AppCompatActivity() {

    private var mLayoutChangeListener: OnGlobalLayoutListener? = null

    private lateinit var flowTest : Flow<Int>

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        operationView()
        scrollView()
        animation2()
        jump_slide_page.setOnClickListener {
            startActivity(Intent(this, SlideMenuLayoutActivity::class.java))
        }

//        val params = view2.layoutParams as ConstraintLayout.LayoutParams
//        params.height = 600
//        view2.layoutParams = params

        bt_move.setOnClickListener {
            startValueAnimation()
        }

        flowTest = flow {
            while (true) {
                 emit(Random(10).nextInt())
                 delay(1000)
            }
        }


        CoroutineScope(Dispatchers.Default).launch {
           flowTest.map {
               it*it
           }.collect {
               Log.i("--",it.toString())
           }
        }

        marquee_textView.setCurrentTextContent("这啧啧啧啧啧啧做做做")
        marquee_textView.startScroll(lifecycle)


        bt_motion.setOnClickListener {
            startActivity(Intent(this,MotionDemoActivity::class.java))
        }
        jump_reveal_page.setOnClickListener {
            startActivity(Intent(this, RevealEffectActivity::class.java))
        }

        bt_preview.setOnClickListener {
            startActivity(Intent(this,PreviewListActivity::class.java))
        }

        bt_compose.setOnClickListener {
            startActivity(Intent(this,ComposeActivity::class.java))
        }

        runBlocking {
            while (true) {
                println("----")
            }
        }

        Log.i("----",getExternalFilesDir(null)!!.absolutePath)
        Log.i("----",getExternalFilesDir(Environment.DIRECTORY_PICTURES)!!.absolutePath)

        Log.i("----",getExternalStorageDirectory()!!.absolutePath)
        infiniteVp()
        initRoate()

        bt_flutter.setOnClickListener {
            startActivity(
                FlutterActivity.createDefaultIntent(this)
            );
        }
    }

    private fun initRoate() {
    }

    override fun onResume() {
        super.onResume()
    }

    fun startValueAnimation() {
//        val params = view2.layoutParams as ConstraintLayout.LayoutParams
//        params.height = 60
//        view2.layoutParams = params

        val diff = view2.top - view1.top
        val oldTop = view2.top
        val animation = ValueAnimator.ofFloat(0f, diff.toFloat())
        animation.addUpdateListener {
            val h =(it.animatedValue as Float)
            view2.top = (oldTop - h).toInt()
        }

        animation.addListener(object:Animator.AnimatorListener{
            override fun onAnimationStart(animation: Animator?) {
            }

            override fun onAnimationEnd(animation: Animator?) {
                view2.bottom = view1.bottom
            }

            override fun onAnimationCancel(animation: Animator?) {
            }

            override fun onAnimationRepeat(animation: Animator?) {
            }
        })
        animation.duration = 300
        animation.start()
    }

    fun closeAnimation() {
         Observable.just("")
             .map {
                 
             }
             .subscribeOn(Schedulers.io())
             .subscribe {

             }
    }

    fun scrollView() {
        move_bt.setOnClickListener {
            scroll_view.startScroller(0, 300)
        }
        move_recover.setOnClickListener {
            scroll_view.startScroller(300, -300)
        }
    }


    fun operationView() {
        iv_rotate.apply {
            addShowView(RotateImage.RotateImageBean(R.drawable.icon_1, 1) {
                Toast.makeText(this@MainActivity, "click image1", Toast.LENGTH_SHORT).show()
            })

            addShowView(RotateImage.RotateImageBean(R.drawable.icon_2, 2) {
                Toast.makeText(this@MainActivity, "click image2", Toast.LENGTH_SHORT).show()
            })

            addShowView(RotateImage.RotateImageBean(R.drawable.icon_2, 3) {
                Toast.makeText(this@MainActivity, "click image2", Toast.LENGTH_SHORT).show()
            })
        }
        bt_rotate.setOnClickListener {
            iv_rotate.apply {
                startRotateAnimation()
            }
        }


        bt_rotate_view.setOnClickListener {
        }
    }


    fun infiniteVp() {
        vp_p.apply {
            val data = arrayListOf("1","2","3")
            val views = arrayListOf<View>()
            data.forEach {
                views.add(TextView(context).apply {
                    text = "${it}"
                })
            }
        }
    }

    fun animation1() {
        val key1= Keyframe.ofFloat(0f, 0f)
        val key2 = Keyframe.ofFloat(0.2f, 0.6f * 1000)
        val key3 = Keyframe.ofFloat(0.5f, 0.7f * 1000)
        val key4 = Keyframe.ofFloat(1f, 1f * 1000)
        val holder = PropertyValuesHolder.ofKeyframe("translationX", key1, key2, key3, key4)
        val animation = ObjectAnimator.ofPropertyValuesHolder(iv_demo, holder)
        animation.startDelay = 1000
        animation.setDuration(1200)
        animation.start()
    }

    fun animation2() {
        iv_demo.animate()
            .translationXBy(500f)
            .translationY(500f)
            .setStartDelay(1500)
            .start()
    }

    private fun testRx(args: Array<String>) {
            val executor = Executors.newFixedThreadPool(10)
//            Observable.range(0, 20)
//                .flatMap { i ->
//                    Observable.create<Int>({ e ->
//                        e.onNext(sleep(i))
//                    })
//                    .subscribeOn(Schedulers.from(executor))
//                }
//                .subscribe { i ->
//                    println(i)
//                }
        }


    private fun cachePictureByGlide() {
//        Glide.with(this)
//            .asBitmap()
//            .load(url)
//            .diskCacheStrategy(DiskCacheStrategy.ALL)
//            .into(image_view);


//        if(Environment.MEDIA_MOUNTED == Environment.getExternalStorageState()
//            || !Environment.isExternalStorageRemovable()) {
//
////外部存储可用
//
//            cachePath = context.getExternalCacheDir().getPath() ;
//
//        }else{
//
////外部存储不可用
//
//            cachePath = context.getCacheDir().getPath() ;
//
//        }
//
//        returncachePath ;

    }

    }

    fun sleep(i : Int): Int{
        Thread.sleep(100)
        return i
    }
